package com.waiting.leetcode;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/21
 **/
public class MaximumAverageSubarrayI643 {
    public static void main(String[] args) {
        int[] nums = {0, 4, 0, 3, 2};
        int k = 1;
        System.out.println(findMaxAverage(nums, k));
    }

    //    public static double findMaxAverage(int[] nums, int k) {
//        Long sum = Long.MIN_VALUE;
//        for (int i = 0; i <= nums.length - k; i++) {
//            Long item = 0l;
//            for (int j = i; j < i + k; j++) {
//                item += nums[j];
//            }
//            sum = Math.max(sum, item);
//        }
//        return Double.valueOf(sum) / k;
//    }
    public static double findMaxAverage(int[] nums, int k) {
        if (nums.length < k) {
            return 0;
        }
        long sum = 0;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        long Max = sum;
        for (int i = k; i < nums.length; i++) {
            sum = sum - nums[i - k] + nums[i];
            Max = Math.max(Max, sum);
        }
        return Double.valueOf(Max) / k;
    }
}
